package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.Order;
import com.itheima.Product;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 斌叔
 * @description: TODO
 * @create 2022-05-14 1:27
 */
@RestController
@Slf4j
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderService orderService;

    @Autowired
    private DiscoveryClient discoveryClient;

//    @Autowired
//    private ProductService productService;

    //准备买一件商品
    @GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) {
        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");

//naco-product微服务的地址 (这是负载均衡以前)
        ServiceInstance instance = discoveryClient.getInstances("service-product").get(0);
//自定义实现挑选服务
//        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
//        int index = new Random().nextInt(instances.size());
//        ServiceInstance serviceInstance = instances.get(index);
        String url = "http://" + instance.getHost() + ":" + instance.getPort()+"/product/"+pid;
//调用商品微服务查询商品信息
        Product product = restTemplate.getForObject(url, Product.class);

//组件负载
        //String url = "service-product";
        //Product product = restTemplate.getForObject("http://" + url + "/product/" + pid, Product.class);
        //log.info(">>调用商品微服务的地址："+url);

//feign调用
//        Product product = productService.findById(pid);


        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());
        order.setNumber(1);
        orderService.save(order);
        return order;

    }

//    //准备买一件商品
//    @GetMapping("/order/prod/{pid}")
//
//    public Order order(@PathVariable("pid") Integer pid) {
//        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");
//
//        //调用商品微服务查询商品信息
//        String url = "http://localhost:8081/product/" + pid;
//        Product product = restTemplate.getForObject(url, Product.class);
//
//        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
//        Order order = new Order();
//        order.setUid(1);
//        order.setUsername("测试用户");
//        order.setPid(product.getPid());
//        order.setPname(product.getPname());
//        order.setPprice(product.getPprice());
//        order.setNumber(1);
//        orderService.save(order);
//        return order;
//
//    }
}
